/**
 * Created with JetBrains WebStorm.
 * User: mumble
 * Date: 6/22/12
 * Time: 10:23 PM
 * To change this template use File | Settings | File Templates.
 */
//var configuration;
//
//var mongoose = require("mongoose");
//
//module.exports.init = function(config)
//{
//    configuration = config;
//};
//module.exports.useDbModel = function(modelName)
//{
//    var checkConnectionExist = (mongoose.connection.readyState === 1 || mongoose.connection.readyState === 2);
//    if(!checkConnectionExist)
//        mongoose.connect(configuration.db_url);
//    return require("./dbmodels/" + modelName + ".js")(mongoose);
//
//}
//
//module.exports.useModule = function (moduleName) {
//    return require("./modules/" + moduleName);
//};


function Service()
{

    var mongoose = require('mongoose');
    var config = null;


    this.init = function(configuration)
    {
        config = configuration;
    };

    this.getConfiguration = function()
    {
        return config;
    };

    this.useDbModel  = function(modelName)
    {
        var checkConnectionExist = (mongoose.connection.readyState === 1 || mongoose.connection.readyState === 2);
        if(!checkConnectionExist)
            mongoose.connect(config.db_url);
        return require("./dbmodels/" + modelName + ".js")(mongoose);
    };

    this.useModule = function(moduleName)
    {
        return require("./modules/" + moduleName);
    };
}

Service.instance = null;

Service.getInstance = function()
{
    if(this.instance === null)
        this.instance = new Service();
    return this.instance;
}
module.exports.Service = Service.getInstance();
